//
//  PreferenceView.swift
//  WalkNovel
//
//  Created by Kevin on 2023/8/29.
//

import UIKit
import Magnetic

class PreferenceView: UIView {
    
    lazy var tipsLabel: UILabel = {
        let label = UILabel()
        label.text = "閱讀之前先完成您的個人資料\n為您提供更多更好的書籍"
        label.textColor = .init(named: "gray_666666")
        label.font = .wn_font(name: .regular, size: 16)
        label.numberOfLines = 0
        return label
    }()
    
    lazy var styleView: MagneticView = {
        let view = MagneticView()
        view.magnetic.magneticDelegate = self
        return view
    }()
    
    lazy var saveBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("確認", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = .wn_font(name: .bold, size: 16)
        btn.backgroundColor = WNColor.Fade().left2right(.init(width: scaleWidth(240), height: 44))
        btn.wn_cornerWithBorder(22)
        return btn
    }()
    
    var selArr = [String]()
        
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(tipsLabel)
        addSubview(saveBtn)
        addSubview(styleView)
        tipsLabel.snp.makeConstraints { make in
            make.top.equalTo(12)
            make.left.equalTo(68)
            make.right.equalTo(-20)
        }
        saveBtn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(-42)
            make.width.equalTo(scaleWidth(240))
            make.height.equalTo(44)
        }
        styleView.snp.makeConstraints { make in
            make.left.right.equalTo(0)
            make.top.equalTo(tipsLabel.snp.bottom).offset(30)
            make.bottom.equalTo(saveBtn.snp.top).offset(-30)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension PreferenceView: MagneticDelegate {
    
    func magnetic(_ magnetic: Magnetic, didSelect node: Node) {
        selArr.append(node.text ?? "")
    }

    func magnetic(_ magnetic: Magnetic, didDeselect node: Node) {
        selArr.removeAll(where: {$0 == node.text})
    }
}
